<template>
  <el-dialog
    :title="!dataForm.telecId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="deviceNumber">
      <el-input v-model="dataForm.deviceNumber" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="acqTime">
      <el-input v-model="dataForm.acqTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentAVoltage">
      <el-input v-model="dataForm.currentAVoltage" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentBVoltage">
      <el-input v-model="dataForm.currentBVoltage" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentCVoltage">
      <el-input v-model="dataForm.currentCVoltage" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentACurrent">
      <el-input v-model="dataForm.currentACurrent" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentBCurrent">
      <el-input v-model="dataForm.currentBCurrent" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentCCurrent">
      <el-input v-model="dataForm.currentCCurrent" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentActivePower">
      <el-input v-model="dataForm.currentActivePower" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentReactivePower">
      <el-input v-model="dataForm.currentReactivePower" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentPowerFactor">
      <el-input v-model="dataForm.currentPowerFactor" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentPositiveActivePower">
      <el-input v-model="dataForm.currentPositiveActivePower" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentNegtiveActivePower">
      <el-input v-model="dataForm.currentNegtiveActivePower" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="currentTotalActivePower">
      <el-input v-model="dataForm.currentTotalActivePower" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="ammeterStatus">
      <el-input v-model="dataForm.ammeterStatus" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          telecId: 0,
          deviceNumber: '',
          acqTime: '',
          currentAVoltage: '',
          currentBVoltage: '',
          currentCVoltage: '',
          currentACurrent: '',
          currentBCurrent: '',
          currentCCurrent: '',
          currentActivePower: '',
          currentReactivePower: '',
          currentPowerFactor: '',
          currentPositiveActivePower: '',
          currentNegtiveActivePower: '',
          currentTotalActivePower: '',
          ammeterStatus: ''
        },
        dataRule: {
          deviceNumber: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          acqTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentAVoltage: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentBVoltage: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentCVoltage: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentACurrent: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentBCurrent: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentCCurrent: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentActivePower: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentReactivePower: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentPowerFactor: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentPositiveActivePower: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentNegtiveActivePower: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          currentTotalActivePower: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          ammeterStatus: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.telecId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.telecId) {
            this.$http({
              url: this.$http.adornUrl(`/xust/ammeterparameter/info/${this.dataForm.telecId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.deviceNumber = data.ammeterParameter.deviceNumber
                this.dataForm.acqTime = data.ammeterParameter.acqTime
                this.dataForm.currentAVoltage = data.ammeterParameter.currentAVoltage
                this.dataForm.currentBVoltage = data.ammeterParameter.currentBVoltage
                this.dataForm.currentCVoltage = data.ammeterParameter.currentCVoltage
                this.dataForm.currentACurrent = data.ammeterParameter.currentACurrent
                this.dataForm.currentBCurrent = data.ammeterParameter.currentBCurrent
                this.dataForm.currentCCurrent = data.ammeterParameter.currentCCurrent
                this.dataForm.currentActivePower = data.ammeterParameter.currentActivePower
                this.dataForm.currentReactivePower = data.ammeterParameter.currentReactivePower
                this.dataForm.currentPowerFactor = data.ammeterParameter.currentPowerFactor
                this.dataForm.currentPositiveActivePower = data.ammeterParameter.currentPositiveActivePower
                this.dataForm.currentNegtiveActivePower = data.ammeterParameter.currentNegtiveActivePower
                this.dataForm.currentTotalActivePower = data.ammeterParameter.currentTotalActivePower
                this.dataForm.ammeterStatus = data.ammeterParameter.ammeterStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/xust/ammeterparameter/${!this.dataForm.telecId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'telecId': this.dataForm.telecId || undefined,
                'deviceNumber': this.dataForm.deviceNumber,
                'acqTime': this.dataForm.acqTime,
                'currentAVoltage': this.dataForm.currentAVoltage,
                'currentBVoltage': this.dataForm.currentBVoltage,
                'currentCVoltage': this.dataForm.currentCVoltage,
                'currentACurrent': this.dataForm.currentACurrent,
                'currentBCurrent': this.dataForm.currentBCurrent,
                'currentCCurrent': this.dataForm.currentCCurrent,
                'currentActivePower': this.dataForm.currentActivePower,
                'currentReactivePower': this.dataForm.currentReactivePower,
                'currentPowerFactor': this.dataForm.currentPowerFactor,
                'currentPositiveActivePower': this.dataForm.currentPositiveActivePower,
                'currentNegtiveActivePower': this.dataForm.currentNegtiveActivePower,
                'currentTotalActivePower': this.dataForm.currentTotalActivePower,
                'ammeterStatus': this.dataForm.ammeterStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
